package Chat.System;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * Interface Utilisateur au démarrage de l'application
 * 
 * @author Ndanja & Papa Abdou
 * 
 */
public class ChatIHMDisconnect extends JFrame implements ActionListener {

    private JButton connection;
    private JLabel msgacceuil;
    private JTextField nicknm;
    private TextArea textToSend;
    private JList jList;
    private ChatGUI gui;
    private JPanel pnPanel;

    /**
     * Le constructeur
     * @param gui: référence du gui
     */
    public ChatIHMDisconnect(ChatGUI gui) {
        super();
        this.gui = gui;
        initComponement();
    }

    public void initComponement() {

        /**
         * GUI a l'acceuil de l'utilisateur
         */
        msgacceuil = new JLabel();
        nicknm = new JTextField();
        connection = new JButton();


        guiDisconnected();

        /* ecoute des actions sur les boutons */
        connection.addActionListener(this);

        //nicknm.addActionListener(this);

        KeyListener myListener = new KeyListener() {

            public void keyTyped(KeyEvent ke) {
            }

            public void keyPressed(KeyEvent ke) {
                if (ke.getKeyCode() == KeyEvent.VK_ENTER) {
                    connection();

                }
            }

            public void keyReleased(KeyEvent ke) {
            }
        };

        nicknm.addKeyListener(myListener);

        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    /**
     * Mise en page
     */
    public void guiDisconnected() {

        pnPanel = new JPanel();
        GridBagLayout gbPanel = new GridBagLayout();

        GridBagConstraints gbcPanel = new GridBagConstraints();
        pnPanel.setLayout(gbPanel);

        msgacceuil.setText("Entrez votre Nickname");
        gbcPanel.gridx = 0;
        gbcPanel.gridy = 0;
        gbcPanel.gridwidth = 1;
        gbcPanel.gridheight = 1;
        gbcPanel.fill = GridBagConstraints.BOTH;
        gbcPanel.weightx = 1;
        gbcPanel.weighty = 1;
        gbcPanel.anchor = GridBagConstraints.NORTH;
        gbPanel.setConstraints(msgacceuil, gbcPanel);
        pnPanel.add(msgacceuil);


        gbcPanel.gridx = 0;
        gbcPanel.gridy = 1;
        gbcPanel.gridwidth = 1;
        gbcPanel.gridheight = 1;
        gbcPanel.fill = GridBagConstraints.BOTH;
        gbcPanel.weightx = 1;
        gbcPanel.weighty = 0;
        gbcPanel.anchor = GridBagConstraints.NORTH;
        gbPanel.setConstraints(nicknm, gbcPanel);
        pnPanel.add(nicknm);


        connection.setText("Connect");
        gbcPanel.gridx = 1;
        gbcPanel.gridy = 1;
        gbcPanel.gridwidth = 1;
        gbcPanel.gridheight = 1;
        gbcPanel.fill = GridBagConstraints.BOTH;
        gbcPanel.weightx = 1;
        gbcPanel.weighty = 0;
        gbcPanel.anchor = GridBagConstraints.NORTH;
        gbPanel.setConstraints(connection, gbcPanel);
        pnPanel.add(connection);



        setContentPane(pnPanel);
        pack();


    }

    /**
     * Action listener pour capturer la touche entrée du bouton  "Connect"
     * @param ae
     */
    @Override
    public void actionPerformed(ActionEvent ae) {

        if ("Connect".equalsIgnoreCase(ae.getActionCommand())) {
            this.connection();
        } else if (ActionEvent.KEY_EVENT_MASK == KeyEvent.VK_ENTER) {
            this.connection();
        } else {
            System.out.println("Pas de connexion");

        }


    }

    /**
     * Capture de la touche entrée dans la zone de texte
     * @param evt
     */
    private void nicknmKeyPressed(java.awt.event.KeyEvent evt) {
        // TODO add your handling code here:
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {

            this.connection();
        }
    }

    public void actionPerformed(KeyEvent ae) {

        if (ae.getKeyCode() == KeyEvent.VK_ENTER) {
            connection();
        }
    }

    /**
     * Connection: fermeture de la fenêtre actuelle et ouverture de la fenêtre Connected
     */
    public void connection() {

        if (!nicknm.getText().isEmpty()) {

            gui.connect(nicknm.getText());

        } else {

            nicknm.setBackground(Color.red);
        }

    }

    /**
     * Fermeture de la fenêtre
     */
    public void exit() {
        this.dispose();
    }
}
